package com.gec.hrm.dao.impl;

import com.gec.hrm.pojo.Notice;
import com.gec.hrm.pojo.Type;
import com.gec.hrm.pojo.User;
import com.gec.hrm.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class NoticeDaoImpl  extends JDBCUtils<Notice> implements NoticeDao{
    @Override
    public List<Notice> noticeList(String name, int page, int limit) {
        String sql = "select * from notice_inf where 1=1 ";//万能条件
//        sql += " limit " + (page - 1) * limit + "," + limit + "";
        List<Notice> notices = query(sql);
        return notices;
    }

    @Override
    public int add(Notice notice) {
        return update("insert into notice_inf  values(null,?,CURRENT_TIMESTAMP,null,?,?,CURRENT_TIMESTAMP)",
                notice.getName(), notice.getContent(),notice.getUserId());
    }

    @Override
    public int delete(int id) {
        return update("delete from notice_inf where id=?",id);
    }

    @Override
    public Notice getBean(ResultSet rs) {
        UserDao userDao = new UserDaoImpl();
        Notice notice = new Notice();
        try {
            notice.setId(rs.getInt("ID"));
            notice.setName(rs.getString("name"));
            notice.setContent(rs.getString("content"));
            notice.setCreateDate(rs.getDate("create_date"));
            notice.setUserId(rs.getInt("user_id"));
            notice.setModifyDate(rs.getDate("modify_date"));
            notice.setTypeId(rs.getInt("type_id"));
            User user = userDao.findById(notice.getUserId());
            if (user != null)
                notice.setUserName(user.getLoginname());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return notice;
    }
}
